﻿#include "gamemanageronline.h"
#include "gamewin.h"

GameManagerOnline::GameManagerOnline
(QTcpSocket *tempSocket, GameSce *scene, QList<Card *> *list, QObject *parent) :
    GameManager(scene, list, parent),
    m_tcpSocket(tempSocket)
{

}

GameManagerOnline::~GameManagerOnline()
{

}

void GameManagerOnline::init()
{
    for(int i = 0; i < m_listCard->count(); i++){
        connect(m_listCard->at(i), &Card::cardDO, this, &GameManagerOnline::Do);
    }
}

void GameManagerOnline::Do(bool humanGroup, int Rotation, Card *card)
{
    Q_UNUSED(humanGroup); // 镇压宏
    int index = m_listCard->indexOf(card);
    int row = index / 4;
    int col = index % 4;
    QString tempStr = "7" + QString::number(row) + QString::number(col)
            + QString::number(Rotation);
    tcpUp(tempStr.toUtf8());
}

void GameManagerOnline::tcpUp(QByteArray bt)
{
    QByteArray btPack;
    QDataStream out(&btPack, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_5_0); // 设置版本，保证跨平台兼容性
    out << quint32(0);

    quint32 btSize = bt.size() + sizeof(quint32);

    out << bt;
    btPack.remove(0, sizeof(quint32));
    out.device()->seek(0);
    out << btSize;

    m_tcpSocket->write(btPack);
}

void GameManagerOnline::tcpUse(QString tempStr)
{
    if(tempStr[0] == "7"){ // 表示是卡牌操作信息
        tempStr.remove(0, 1);
        int row = tempStr.mid(0, 1).toInt();
        int col = tempStr.mid(1, 1).toInt();
        int Rot = tempStr.mid(2, 1).toInt();
        int index = row * 4 + col;
        disconnect(m_listCard->at(index), &Card::cardDO,
                   this, &GameManagerOnline::Do);
        switch (Rot) {
        case 0:
            m_listCard->at(index)->openAnimation();
            break;
        case 1:
            m_listCard->at(index)->upAnimation();
            break;
        case 2:
            m_listCard->at(index)->downAnimation();
            break;
        case 3:
            m_listCard->at(index)->leftAnimation();
            break;
        case 4:
            m_listCard->at(index)->rightAnimation();
            break;
        default:
            break;
        }
        connect(m_listCard->at(index), &Card::cardDO,
                           this, &GameManagerOnline::Do);
    }
}
